<?php
/**
 * 商户申请管理所有
 */
$up_url="../";
include (dirname(__FILE__)."/session.inc.php");//全局变量

//页面属于
$mod=trim(empty($_REQUEST['mod'])?"list":$_REQUEST['mod']);
$smarty->assign("mod",$mod);
$link_url="";
$tb_ad_img="`".$tbprefix."ad_img`";
/*分页数*/
$pagesize =5;
//当前页
$page =empty($_GET['page'])||($_GET['page']<0)?"1":trim($_GET['page']);

switch ($mod){
	//信息编辑
	case "edit":

		//页面动作
		@$act=isset($_REQUEST['act'])?trim($_REQUEST['act']):"";
		@$back_url=$_SESSION['act_url']?$_SESSION['act_url']:"?mod=list";
		$pid=isset($_GET['pid'])?$_GET['pid']:"0";
		$smarty->assign("pid",$pid);
		
		switch ($act){
			//添加
			case "add":
			
				$titles_edit="添加广告位图片";
				$oneinfo=array("id"=>0);
			
				break;
			//更新
			case "update":

				$titles_edit="修改广告位图片";
				//商户信息
				$oneinfo=$db->get_one("SELECT `id`,`type`, `img_name`, `img_url`,`img_src`, `is_valid`, `orderid`, `is_show` FROM ".$tb_ad_img." WHERE `id`='".$pid."' AND `is_valid`='1' AND 1");
				
				if(empty($oneinfo['id'])){
					$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
					break;
				}
				break;

				//修改
			case "change":
				$hid=isset($_POST['actId'])?$_POST['actId']:"";
				
				$dataArray['img_name']=$fun->checkInput(trim($_POST['img_name']?$_POST['img_name']:""));
				$dataArray['img_url']=$fun->checkInput(trim($_POST['img_url']?$_POST['img_url']:""));
				$dataArray['is_show']=$fun->checkInput(trim(isset($_POST['is_show'])?$_POST['is_show']:"0"));
				$dataArray['orderid']=$fun->checkInput(trim(isset($_POST['orderid'])?$_POST['orderid']:"0"));
				$dataArray['type']=$fun->checkInput(trim(isset($_POST['type'])?$_POST['type']:1));
				
				//传递过来的url
				$img_src_arr_str=trim(isset($_POST['img_src'])?$fun->checkInput($_POST['img_src']):"");
				$img_src_arr=empty($img_src_arr_str)?"":explode($cfg['uploadfile'], $img_src_arr_str);
				$img_src=$cfg['uploadfile'].(empty($img_src_arr)?"":$img_src_arr[1]);
				if (empty($img_src)||$img_src==$cfg['uploadfile']) {
					$fun->msg("没有图片,正在跳转...","",1);//错误跳转到新的页面
				}
				$dataArray['img_src']=$img_src;
				
				if($hid){
					//更新
					$dataArray['change_time']=$m_now_time;//修改数据时间
					if($db->update($tb_ad_img,$dataArray,$condition="`id`='".$hid."' AND 1"))
					{
						$fun->msg('更新成功','?mod=edit&act=update&pid='.$hid.'&page='.$page,1);
						$db->close();
					}
					else
					{
						$fun->msg('更新失败','?mod=edit&act=update&pid='.$hid.'&page='.$page,1);
					}
				}else{
					//添加
					$dataArray['addtime']=$m_now_time;
					$dataArray['is_valid']=1;//有用数据
					if($db->insert($tb_ad_img,$dataArray))
					{
						$fun->msg('添加成功','?mod=edit&act=add&page='.$page,1);
						$db->close();
					}
					else
					{
						$fun->msg('添加失败','?mod=edit&act=add&page='.$page,1);
					}
				}
				break;

			default:
				$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
				break;
		}


		$smarty->assign("one",$oneinfo);//要修改的信息
		$smarty->assign("titleAct",$titles_edit);//页面操作动作标题
		$smarty->assign("Returnfpage",$back_url);//返回列表

		break;
	
	case "delone"://单个删除
		
			@$back_url=$_SESSION['act_url']?$_SESSION['act_url']:"?mod=list";
			$id=!empty($_REQUEST['delete_id'])?$fun->checkInput($_REQUEST['delete_id']):"";
			$users_del_info=$db->get_one("SELECT `id`,`img_url` FROM `{$tbprefix}ad_img` WHERE `id`='{$id}' AND `is_valid`='1' AND 1");
			/*单个删除*/
			if($users_del_info['id']){
				$dataArray['del_time']=$m_now_time;
				$dataArray['del_cause']=!empty($_REQUEST['del_cause'])?$fun->checkInput($_REQUEST['del_cause']):"";
				$dataArray['is_valid']="0";
				$del_cata=$db->update($table=" `{$tbprefix}ad_img` ",$dataArray,$condition="`id`='".$id."' AND 1");
				if($del_cata==true){
					//记录操作
					$fun->msg("删除成功",$back_url,1);
				}
				else{
					$fun->msg("删除失败",$back_url,1);
				}
			}else{
				$fun->msg("操作失败,正在跳转...",$back_url,1);//错误跳转到新的页面
			}
		
			break;
			
	//是否显示
	case "ishow":
		/*更新是否显示*/
		@$back_url=$_SESSION['act_url']?$_SESSION['act_url']:"?mod=list";
		if($adminPower<1){
			$id=empty($_GET['id'])?0:trim($_GET['id']);
			$user_total=$db->get_one("SELECT COUNT(`id`) AS `total` FROM `{$tbprefix}ad_img` WHERE `id`='$id' AND `is_valid`='1' AND 1");
			switch ($user_total['total']){
				case 0:
					//管理员权限
					$fun->msg("无效的广告位,正在跳转...",$back_url,1);
					break;
				default:
					$ishow=isset($_GET['ishowId'])?$_GET['ishowId']:($fun->msg("操作失败,正在跳转...",$back_url,1));
					$db->query("UPDATE `{$tbprefix}ad_img` SET `is_show`='".$ishow."' WHERE `id`='".$id."' AND 1");
					echo "<script>window.location.href='$back_url';</script>";
					break;
			}
			}else{
			$fun->msg("权限不足",$back_url,1);//错误跳转到新的页面
			}
			break;

		//信息列表
	default:

		$condition="";

		$keyword=trim(empty($_GET['keyword'])?"":$fun->checkInput($_GET['keyword']));
		$is_show=trim(empty($_GET['is_show'])?"0":$_GET['is_show']);
		$get_type=trim(empty($_GET['get_type'])?"0":$_GET['get_type']);
		$t1=trim(empty($_GET['t1'])?"":$_GET['t1']);
		$t2=trim(empty($_GET['t2'])?"":$_GET['t2']);
		if($keyword){
			$keywords = str_replace(" ","%",trim(urldecode($keyword)));
			$condition.="((s.`img_name` LIKE '%".$keywords."%')) AND ";
		}
		$smarty->assign("keyword",$fun->showInput($keyword, ENT_QUOTES, "UTF-8" ));
		
		if($t1){
			$ts1=strtotime($t1);
			$condition.="(s.`addtime`>'".$ts1."') AND ";
		}
		$smarty->assign("t1",$t1);
		if($t2){
			$ts2=strtotime($t2." 23:59:59");
			$condition.="(s.`addtime`<'".$ts2."') AND ";
		}
		$smarty->assign("t2",$t2);
		
		if($is_show>0){
			$is_shows = ($is_show-1>0)?1:0;
			$condition.="(s.`is_show`='".$is_shows."') AND ";
		}
		$smarty->assign("is_show",$is_show);
		
		if($get_type){
			$condition.="(s.`type`='".intval($get_type)."') AND ";
		}
		$smarty->assign("get_type",$get_type);
		
		$fpage = $fun->setPageUri();//该页面;

		$mer_sql="SELECT s.`id`,s.`type`, s.`img_name`, s.`img_url`,s.`img_src`, s.`orderid`,s.`is_show`, s.`addtime`, s.`change_time` FROM `{$tbprefix}ad_img` AS s WHERE {$condition} s.`is_valid`='1' AND 1 ORDER BY  s.orderid DESC,s.`addtime` DESC";
		/*--分页开始--*/
		
		$numpage = $db->pagedeal($mer_sql, $pagesize);//调用分页处理函数

		//----------------------分页相关的模板变量替换-------------------------//
		$smarty->assign("recordcount", $recordcount); 	//总记录数
		$smarty->assign("pagecount", $pagecount);  		//总页数
		$smarty->assign("prepage", $page - 1);     		//上一页
		$smarty->assign("nexpage", $page + 1);     		//下一页
		$smarty->assign("numpage", $numpage);    		//数字分页

		//SQL LIMIT起始
		$startloc=(($page-1)*$pagesize<0)?0:($page-1)*$pagesize;

		$merValue=$db->get_all($mer_sql." LIMIT $startloc,$pagesize");
		
		$i=0;											//定义序号初始为0
		$ad_img_arr=array();
		foreach ($merValue as $key=>$value){
			//序号
			$i++;$pageid=(($page-1)*$pagesize)+$i;
			$ad_img_arr[$key]=$value;
			//时间
			$addtimeDate=$value['addtime']?date("Y-m-d H:i:s",$value['addtime']):"0000-00-00 00:00:00";
			if($t1 || $t2){
				$addtimeDate=str_ireplace($addtimeDate,"<span class=\"xinghao\">".$addtimeDate."</span>",$addtimeDate);
			}
			//==查询描红加粗==//
			$ad_img_arr[$key]['pageid']=$pageid;
			$ad_img_arr[$key]['addtime']=$addtimeDate;
			$ad_img_arr[$key]['change_time']=$value['change_time']?date("Y-m-d H:i:s",$value['change_time']):"0000-00-00 00:00:00";
			$ad_img_arr[$key]['img_name']=$fun->showInput($value['img_name'],$ent=ENT_QUOTES);
			if ($value["type"]==1) {
				$ad_img_arr[$key]['type'] = "地图广告";
			}elseif ($value["type"]==2) {
				$ad_img_arr[$key]['type'] = "首页广告";
			}
		}//print_r($merchant_arr);
		$smarty->assign("ad_img_arr",$ad_img_arr);			
		$act_url=$_SESSION['act_url']=$fun->GetCurUrl(1);//获取URL的路径存入session
		
		
		/*批量更新排序*/
		if(!empty($_POST['replace'])){
			$all_id=isset($_POST['all'])?$_POST['all']:"";//所有id
			if($adminPower<1){
				if(isset($all_id)){
					$order_id=isset($_POST['rep'])?$_POST['rep']:"";//更新的排序id
					foreach($all_id as $key=>$value){
						$db->query("UPDATE `{$tbprefix}ad_img` SET `orderid`='".$order_id[$key]."' WHERE `id`='".$value."' AND 1");
					}
					echo "<script>window.location.href='$act_url';</script>";
				}
			}else{
				$fun->msg("权限不足",$act_url,1);//错误跳转到新的页面
			}
		}
		
		$smarty->assign("fpage", $fpage); 	//要分页的网页

	break;
}

$smarty->assign("page",$page);
$smarty->assign("title",$cfg['sysName']);
@$smarty->display("ad_img.tpl");
?>